package ldkxingzhe.l365calendar.fragment.presenter;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;

import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ldkxingzhe.l365calendar.App;
import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.dao.NoteDao;
import ldkxingzhe.l365calendar.model.NoteEntity;
import ldkxingzhe.l365calendar.utils.CalendarUtil;
import ldkxingzhe.l365calendar.widget.dialog.picker.DatePicker;

/**
 * 待办事项的Presenter
 * Created by xingzhe on 16/1/28.
 */
public class NoteEditPresenter {
    @SuppressWarnings("unused")
    private static String TAG = "NoteEditPresenter";

    /**
     * 获取当前截止时间
     */
    public long getLongTime() {
        Pattern pattern = Pattern.compile("\\d{4}-\\d\\d-\\d\\d");
        Matcher matcher = pattern.matcher(mView.getDeadLineText());
        if(matcher.find()){
            try {
                return CalendarUtil.parseTime(matcher.group(), CalendarUtil.DATE).getTime();
            } catch (Exception e) {
                Log.e(TAG, e.getMessage(), e);
            }
        }
        return -1;
    }

    /**
     * 当截止时间返回结果时
     * @param resultCode    结果码
     * @param data          返回的intent
     */
    public void onDeadlineSelect(int resultCode, Intent data) {
        if(resultCode != Activity.RESULT_OK) return;
        if(data == null){
            Log.e(TAG, "data should not be null");
            return;
        }

        String timeStr = data.getStringExtra(DatePicker.RESULT);
        try {
            Date resultDate = CalendarUtil.parseTime(timeStr, CalendarUtil.DATE);
            String day;
            if(DateUtils.isToday(resultDate.getTime())){
                day = App.applicationContext.getString(R.string.today);
            }else{
                int index = resultDate.getDay() - 1;
                if(index == -1) index = 6;
                day = "周" + mView.getDayName(index);
            }
            String deadlineText = timeStr + " " + day + "  截止";
            mView.setDeadlineText(deadlineText);
        } catch (ParseException e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    public boolean onSave() {
        if(TextUtils.isEmpty(mView.getContent())){
            mView.showAlert(R.string.content_is_null);
            return false;
        }
        NoteEntity entity = new NoteEntity();
        entity.setTitle(mView.getContent());
        long longTime = new Date().getTime();
        entity.setCreated(longTime);
        entity.setModified(longTime);
        entity.setState(0);
        long deadline = getLongTime();
        if(deadline != -1)
            entity.setDeadline(deadline);
        int priority = isImportant ? 1 : 0;
        entity.setPriority(priority);
        NoteDao noteDao = new NoteDao(App.applicationContext);
        noteDao.insert(entity);
        return true;
    }

    public interface PView{
        void setIsChanged(boolean isChanged);
        void importToDoResource(int resrouceId);
        void setDeadlineText(String text);
        String getDayName(int index);
        String getDeadLineText();
        String getContent();
        void showAlert(int stringId);
    }
    private PView mView;
    public void setView(PView view){
        mView = view;
    }

    private boolean isImportant = false;
    public void onClickImportToDo(){
        isImportant = !isImportant;
        int resourceId = R.drawable.note_priority_unchecked_star;
        if(isImportant){
            resourceId = R.drawable.note_priority_checked_star;
        }
        mView.importToDoResource(resourceId);
    }
}
